package com.axzhengxin.platform.afterloan.controller;

import com.axzhengxin.platform.afterloan.domain.RiskSignalTableData;
import com.axzhengxin.platform.afterloan.service.IFspCorInfoService;
import com.axzhengxin.platform.afterloan.service.IRiskModelService;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * WarnSignalInfoController
 * 预警信号列表接入点
 *
 * @author jiangnan
 * @date 2021/3/22
 */
@RequestMapping(path = "/afterloan/warn-signal")
@RestController
public class WarnSignalInfoController extends BaseController {

    private IRiskModelService riskModelService;

    private IFspCorInfoService fspCorInfoService;

    public WarnSignalInfoController(IRiskModelService riskModelService,
                                    IFspCorInfoService fspCorInfoService) {
        this.riskModelService = riskModelService;
        this.fspCorInfoService = fspCorInfoService;
    }

    /*@PreAuthorize("@ss.hasPermi('afterloan:warnlist:list')")*/
    @GetMapping(path = "/list")
    public TableDataInfo list(RiskSignalTableData params) {
        startPage();
        return getDataTable(riskModelService.selectRiskSignalList(params));
    }



    /*@PreAuthorize("@ss.hasPermi('afterloan:warnlist:info')")*/
    @GetMapping(path = "/detail-list/{customerId}")
    public TableDataInfo warnList(@PathVariable("customerId") String customerId) {
        return getDataTable(riskModelService.warnList(customerId));
    }



    /*@PreAuthorize("@ss.hasPermi('afterloan:warnlist:disable')")*/
    @GetMapping(path = "/warn-list/disable/{signalId}")
    public AjaxResult disableWarnSignal(@PathVariable("signalId") Integer id) {
        return toAjax(riskModelService.disableWarnSignal(id));
    }

    @GetMapping(path = "/{customerId}")
    public AjaxResult detail(@PathVariable("customerId") String customerId) {
        return Optional.ofNullable(fspCorInfoService.getCustomerInfoById(customerId))
                .map(AjaxResult::success)
                .map(ajax -> {
                    // 将所有该客户的预警信号设置为已查看
                    riskModelService.disableWarnSignal(customerId);
                    return ajax;
                })
                .orElse(new AjaxResult(HttpStatus.NOT_FOUND, "未找到指定用户"));
    }

}
